package com.nxu.dao.impl;

import com.nxu.dao.UsersDao;
import com.nxu.dao.BaseDao;
import com.nxu.entity.UserBean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UsersDaoImpl extends BaseDao implements UsersDao {

    @Override
    public UserBean isLogin(String username, String pwd, int userType) {
        UserBean user = null;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = this.getConn();
            String sql = "SELECT id, username, pwd, utype FROM users WHERE username = ? AND pwd = ? AND utype = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, pwd);
            pstmt.setInt(3, userType);

            rs = pstmt.executeQuery();
            if (rs.next()) {
                user = new UserBean();
                user.setUserId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("pwd"));
                user.setUserType(rs.getInt("utype"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(conn, pstmt, rs);
        }
        return user;
    }
}